package com.sztx.pay.center.rpc.component;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.sztx.pay.center.common.util.ValidateUtil;
import com.sztx.pay.center.common.util.Constance.PayFlowState;
import com.sztx.pay.center.common.util.Constance.PayFlowType;
import com.sztx.pay.center.core.service.OrderMappingService;
import com.sztx.pay.center.core.service.PayCoreService;
import com.sztx.pay.center.core.service.bean.PayCoreResult;
import com.sztx.pay.center.dataaccess.domain.UserActDO;
import com.sztx.pay.center.dataaccess.domain.UserBindCardDO;
import com.sztx.pay.center.rpc.api.domain.TradeBaseRequest;
import com.sztx.pay.center.rpc.api.domain.TradeResponse;
import com.sztx.trade.payment.gateway.common.trade.constant.TradeStatus;

public class VerifyQuickPayService extends TradeService {
	
	@Autowired
	PayCoreService payCoreService;
	
	@Autowired
	OrderMappingService orderMappingService;

	@Override
	void checkPrivateParam(Map<String, String> params) {
		String tradeOrderId = params.get("tradeOrderId");
		String verifyCode = params.get("verifyCode");
		
		
		ValidateUtil.isNotBlank(tradeOrderId, "tradeOrderId不能为空");
		ValidateUtil.isNotBlank(verifyCode, "verifyCode不能为空");
	}

	@Override
	boolean isRepeatOrder(Map<String, String> params) {
		return false;
	}

	@Override
	String recordOrder(TradeBaseRequest request) {
		return null;
	}

	@Override
	UserBindCardDO getPayerBindInfo(Map<String, String> params) {
		return null;
	}

	@Override
	UserBindCardDO getPayeeBindInfo(Map<String, String> params) {
		return null;
	}

	@Override
	UserActDO getPayerActInfo(TradeBaseRequest request) {
		return null;
	}

	@Override
	UserActDO getPayeeActInfo(TradeBaseRequest request) {
		return null;
	}

	@Override
	TradeResponse invokePayCore(Map<String, String> params) {
		
		return verify(params);
	}
	
	
	public TradeResponse verify(Map<String, String> params) {
		
		String tradeOrderId = params.get("tradeOrderId");
		String verifyCode = params.get("verifyCode");
		
		String referOrderId = orderMappingService.getMappingOrderId(tradeOrderId, (byte) PayFlowType.PAY.getCode());
		PayCoreResult result = payCoreService.verifyQuickPayTrade(referOrderId, verifyCode);
		
		TradeResponse response = new TradeResponse();
		response.setResultMsg(result.getTradeDesc());
		if (tradeOrderId != null && (tradeOrderId.startsWith("14") || tradeOrderId.startsWith("11"))){  //充值单和B2C订单，同步返回支付中
			if (result.getTradeSataus() == TradeStatus.TS_SUCCEEDED) {
				response.setResultCode("2");
				response.setResultMsg(PayFlowState.PAYING.getDesc());
			} else {
				response.setResultCode(Integer.toString(result.getTradeSataus()));
			}
		} else {
			response.setResultCode(Integer.toString(result.getTradeSataus()));
		}
//		response.setResultCode(Integer.toString(result.getTradeSataus()));
		response.setTradeOrderId(tradeOrderId);
		return response;
	}

	@Override
	void handerPayCoreResult() {
		

	}

}
